//
//  StockInfoViewController.m
//  HeXun
//
//  Created by 8003 on 15/11/21.
//  Copyright © 2015年 LGC. All rights reserved.
//

#import "StockInfoViewController.h"
#import "GetXMLObject.h"
#import "MBProgressHUD.h"
#import "SearchTableViewController.h"
#import "StockModel.h"
#import "StockInfoModelViewController.h"
@interface StockInfoViewController ()<UITextFieldDelegate>
{
    NSInteger _blockID;
    
    //搜索框
    __weak IBOutlet UITextField *_searchTextField;
}
@end

@implementation StockInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化模型
    self.stockModelView = [StockModelView new];
    self.stockModelView.tag = 2;
    self.stockModelView.frame = CGRectMake(0, 94, self.view.frame.size.width,self.view.frame.size.height);
    //把模型视图添加到父视图上
    [self.view addSubview:self.stockModelView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(action:) name:@"transfrom" object:nil];

    //设置搜索单行文本框代理
    _searchTextField.delegate = self;
    _searchTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    //添加键盘附件栏
    UIButton *returnButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 60)];
    [returnButton setTitle:@"返     回" forState:UIControlStateNormal];
    [returnButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    returnButton.titleLabel.font = [UIFont systemFontOfSize:25];
    returnButton.backgroundColor = [UIColor lightGrayColor];
    [returnButton addTarget:self action:@selector(returnButtonTouch) forControlEvents:UIControlEventTouchUpInside];
    
    _searchTextField.inputAccessoryView = returnButton;
    
    
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //这是通过代码块自定义操作的operation的子类
    NSBlockOperation* cOperation = [NSBlockOperation blockOperationWithBlock:^{
        GetXMLObject* getXMLObject = [GetXMLObject new];
        

        if (self.segmentedControllerIndex == 1)
        {
            switch (self.index)
            {
                case 0:
                {
                    _blockID = 252;
                }
                    break;
                case 1:
                {
                    _blockID = 1;
                }
                    break;
                case 2:
                {
                    _blockID = 2;
                }
                    break;
                case 3:
                {
                    _blockID = 5;
                }
                    break;
                case 4:
                {
                    _blockID = 6;
                }
                    break;
                case 5:
                {
                    _blockID = 3;
                }
                    break;
                case 6:
                {
                    _blockID = 4;
                }
                    break;
                    
                default:
                    break;
            }
            NSArray* array = [getXMLObject getStockArrayFromBlock:_blockID sort:4 type:0 start:0 end:20];
            self.stockModelView.stockArray = array;
        }
        
        if (self.segmentedControllerIndex == 2)
        {
            switch (self.index)
            {
                case 0:
                {
                    _blockID = 248;
                }
                    break;
                case 1:
                {
                    _blockID = 23;
                }
                    break;
                
                default:
                    break;
            }
            NSArray* array = [getXMLObject getStockArrayFromBlock:_blockID sort:4 type:0 start:0 end:20];
            self.stockModelView.stockArray = array;
        }
        
        if (self.segmentedControllerIndex == 3)
        {
            self.stockModelView.infoLabel.text = @"       最新价     |     涨幅    |    涨跌    |    行情  |    成交量  |    成交额  |    量比    |    涨速%   |    振幅%    |    昨收  ";
            NSArray* array = [getXMLObject getOutFutureGoodsArray];
            self.stockModelView.stockArray = array;
        }
        
        if (self.segmentedControllerIndex == 4)
        {
            self.stockModelView.infoLabel.text = @"       最新价     |     涨幅    |    涨跌    |    行情  |    成交量  |    成交额  |    量比    |    涨速%   |    振幅%    |    昨收  ";
            NSArray* array = [getXMLObject getFundNetValueArray];
            self.stockModelView.stockArray = array;
        }
        
        if (self.segmentedControllerIndex == 5)
        {
            self.stockModelView.infoLabel.text = @"       最新价     |     涨幅    |    涨跌    |    行情  |    成交量  |    成交额  |    量比    |    涨速%   |    振幅%    |    昨收  ";
            NSArray* array = [getXMLObject getExchangeQuotationsArray];
            self.stockModelView.stockArray = array;
        }
        
        if (self.segmentedControllerIndex == 6)
        {
            self.stockModelView.infoLabel.text = @"       最新价     |     涨幅    |    涨跌    |    行情  |    成交量  |    成交额  |    量比    |    涨速%   |    振幅%    |    昨收  ";
            NSArray* array = [getXMLObject getExchangeQuotationsArray];
            self.stockModelView.stockArray = array;
        }


        
        if (self.segmentedControllerIndex == 7)
        {
            self.stockModelView.infoLabel.text = @"       最新价     |     涨幅    |    涨跌    |    行情  |    成交量  |    成交额  |    量比    |    涨速%   |    振幅%    |    昨收  ";
            NSArray* array = [getXMLObject getAllAroundtheWorldStock];
            switch (self.index) {
                case 0:
                {
                    NSMutableArray* tempArray = [NSMutableArray new];
                    for (StockModel* stockModel in array)
                    {
                        NSLog(@"%@",stockModel.fld_subareas);
                        if ([stockModel.fld_subareas isEqualToString:@"亚洲国家"])
                        {
                            [tempArray addObject:stockModel];
                        }
                    }
                    NSArray* arr = [NSArray arrayWithArray:tempArray];
                    self.stockModelView.stockArray = arr;
                }
                    break;
                case 1:
                {
                    NSMutableArray* tempArray = [NSMutableArray new];
                    for (StockModel* stockModel in array)
                    {
                        if ([stockModel.fld_subareas isEqualToString:@"美洲国家"])
                        {
                            [tempArray addObject:stockModel];
                        }
                    }
                    NSArray* arr = [NSArray arrayWithArray:tempArray];
                    self.stockModelView.stockArray = arr;
                }
                    break;
                case 2:
                {
                    NSMutableArray* tempArray = [NSMutableArray new];
                    for (StockModel* stockModel in array)
                    {
                        if ([stockModel.fld_subareas isEqualToString:@"欧洲国家"])
                        {
                            [tempArray addObject:stockModel];
                        }
                    }
                    NSArray* arr = [NSArray arrayWithArray:tempArray];
                    self.stockModelView.stockArray = arr;

                }
                    break;
                case 3:
                {
                    NSMutableArray* tempArray = [NSMutableArray new];
                    for (StockModel* stockModel in array)
                    {
                        if ([stockModel.fld_subareas isEqualToString:@"中东国家"])
                        {
                            [tempArray addObject:stockModel];
                        }
                    }
                    NSArray* arr = [NSArray arrayWithArray:tempArray];
                    self.stockModelView.stockArray = arr;

                }
                    break;
                case 4:
                {
                    NSMutableArray* tempArray = [NSMutableArray new];
                    for (StockModel* stockModel in array)
                    {
                        if ([stockModel.fld_subareas isEqualToString:@"非洲国家"])
                        {
                            [tempArray addObject:stockModel];
                        }
                    }
                    NSArray* arr = [NSArray arrayWithArray:tempArray];
                    self.stockModelView.stockArray = arr;

                }
                    break;
               
                default:
                    break;
            }
        }


        
        
        
        NSBlockOperation* dOperation = [NSBlockOperation blockOperationWithBlock:^{
            [self.stockModelView.table1 reloadData];
            [self.stockModelView.table2 reloadData];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
        //涉及更新UI的操作，就要创建包含这些操作的Operation对象，并把它放入主线程队列
        NSOperationQueue* mainQueue = [NSOperationQueue mainQueue];
        //当Operation放入主线程队列中，其操作就是子主线程中执行
        [mainQueue addOperation:dOperation];
    }];
    
    //创建Operation队列,operation只有在队列里才能跑
    NSOperationQueue* takeMoneyQueue = [NSOperationQueue new];
    
    [takeMoneyQueue addOperation:cOperation];
}



-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"transfrom" object:nil];
}

#pragma mark - 跳转到stockInfoModelViewController的方法
- (void)action:(NSNotification* )notification
{
    if ([notification.object isKindOfClass:[StockModelView class]])
    {
        StockModelView* stockModelView = notification.object;
        if (stockModelView.tag == 2)
        {
            if (self.segmentedControllerIndex != 0)
            {
                UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                StockInfoModelViewController* stockInfoModelViewController = [storyboard instantiateViewControllerWithIdentifier:@"stockInfoViewModel"];
                NSLog(@"%d",[self.stockModelView.table2 indexPathForSelectedRow].row);
                stockInfoModelViewController.segmentIndex = self.segmentedControllerIndex;
                stockInfoModelViewController.pointArr = self.stockModelView.stockArray[[self.stockModelView.table2 indexPathForSelectedRow].row ];
                [self.navigationController pushViewController:stockInfoModelViewController animated:YES];
            }
        }
    }
}


#pragma mark - 搜索框回收键盘的方法
-(void)returnButtonTouch
{
    [_searchTextField resignFirstResponder];
}


#pragma mark - 限制搜索框输入的内容不超过6个字符
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //利用传入的参数“预测”文本改变之后的结果
    NSString *futureText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    //限制字符串最大长度为6
    if (futureText.length <= 6)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}



#pragma mark - 搜索按钮触发的方法
- (IBAction)searchButtonTouch:(UIButton *)sender
{
    SearchTableViewController *searchController = [SearchTableViewController new];
    searchController.searchBarText = _searchTextField.text;
    
    //跳转到搜索结果控制器
    [self.navigationController pushViewController:searchController animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
